package com.igeek.clud_system.service;

import com.igeek.clud_system.common.Const;
import com.igeek.clud_system.dao.*;
import com.igeek.clud_system.entity.*;
import com.igeek.clud_system.util.DateUtil;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class ActivityService {
    @Autowired
    ActivityDao activityDao;
    @Autowired
    ClubDao clubDao;
    @Autowired
    StudentDao studentDao;
    @Autowired
    ClubMemberDao clubMemberDao;
    @Autowired
    HttpSession session;
    @Autowired
    CommentDao commentDao;
    @Autowired
    ActivityAttendDao activityAttendDao;
    @Autowired
    InformationDao informationDao;
    /**
     * 查询所有活动
     */
    @Transactional
    public List<Activity> findActivity(){
        List<Activity> list = activityDao.findAll();
        if(list !=null){
            return list;
        }
        else {
            return null;
        }
    }

    /**
     * 根据社团id查询社团信息
     * @param clubId
     * @return
     */
    @Transactional
    public Club findClub(Integer clubId){
        Club club = clubDao.findClubById(clubId);
        if (club != null){
            return club;
        }
        else {
            return null;
        }
    }

    /**
     *  根据活动id查询活动信息
     * @param activityId
     * @return
     */
    @Transactional
    public Activity findActivityById(Integer activityId){
        Activity activity = activityDao.findById(activityId);
        if (activity != null){
            return activity;
        }
        else {
            return null;
        }
    }

    /**
     * 模糊查询活动
     * @param activityName
     * @param clubName
     * @return
     */
    @Transactional
    public List<Activity> searchActivities(String activityName, String clubName) {
        return activityDao.searchActivities(activityName, clubName);
    }


    /**
     * 根据社团id查询社长信息
     * @param clubId
     * @return
     */
    @Transactional
    public Student findPre(Integer clubId){
        ClubMembers president = clubMemberDao.findClubPresident(clubId);
         return   studentDao.findByStudentId(president.getStudentId());
    }
    /**
     * 根据社团id查询管理员信息
     * @param clubId
     * @return
     */
    @Transactional
    public List<Student> findAdmin(Integer clubId){
        List<Student> list = new ArrayList<>();
        // 查询管理员
        List<ClubMembers> clubAdmin = clubMemberDao.findClubAdmin(clubId);
        for (ClubMembers members : clubAdmin) {
            Student astudent = studentDao.findByStudentId(members.getStudentId());
            //  把管理员添加到list中
            list.add(astudent);
        }
        return list;
    }

    /**
     *  插入活动
     * @param activity
     */
    @Transactional
    public void insertActivity(Activity activity){
        // 获取当前用户
        Student student = (Student) session.getAttribute(Const.LOGIN_USER);
        if (student!= null){
            ClubMembers clubMember = clubMemberDao.findByStudentId(student.getStudentId());
            // 写入社团id
            activity.setClubId(clubMember.getClubId());
        }else {
            throw new RuntimeException("未登录");
        }
        // 活动状态默认1(未审批，)
        activity.setActivityStatus(3);
        // 活动分数默认0
        activity.setActivityGrade(0);
        // 活动验证码随机4位数字
        activity.setActivityCode(String.valueOf((int)((Math.random()*9+1)*1000)));
        // 写入活动信息
        activityDao.insertActivity(activity);
    }
    /**
     * 插入评论
     * @param comment
     */
    @Transactional
    public void insertComment(Comment comment) {
        // 获取当前用户
        Student student = (Student) session.getAttribute(Const.LOGIN_USER);
        if (student!= null){
            comment.setStudentId(student.getStudentId());
        }else {
            throw new RuntimeException("未登录");
        }
        //插入评论
        commentDao.insertComment(comment);
    }

    /**
     * 根据活动id获取评论
     * @param activityId
     * @return
     */
    public List<Comment> findComments(Integer activityId) {
        return commentDao.findCommentByActivityId(activityId);
    }

    /**
     *  根据社团id查询社团活动
     * @param clubId
     * @return
     */
    public List<Activity> findAllByClubId(Integer clubId) {
        return activityDao.findAllByClubId(clubId);
    }


    /**
     *  根据活动id查询活动成员
     * @param activityId
     * @return
     */
    @Transactional
    public List<Student> findActivityMembers(Integer activityId) {
        List<Student> list = new ArrayList<>();
        for (ActivityAttend member : activityAttendDao.findByActivityMembers(activityId)) {
            ActivityAttend activityAttend = activityAttendDao.findByAttendId(member.getActivityId(), member.getStudentId());
            Student student = studentDao.findStudentById(activityAttend.getStudentId());
            list.add(student);
        }
        return list;
    }

    // 更新成员的参与状态
    @Transactional
    public void updateMemberStatus(Integer activityId, Integer studentId, Integer status) {
        ActivityAttend activityAttend = new ActivityAttend();
        activityAttend.setActivityId(activityId);
        activityAttend.setStudentId(studentId);
        activityAttend.setStatus(status);
        activityAttendDao.updateMemberStatus(activityAttend);
    }

    /**
     *  根据社团id和活动id查询社团成员
     * @param clubId
     * @param activityId
     * @return
     */
    @Transactional
    public List<Student> findByClubIdAndActivityId(Integer clubId, Integer activityId) {
        List<Student> list = new ArrayList<>();
        List<ActivityAttend> activityAttendList = activityAttendDao.findByClubIdAndActivityId(clubId, activityId);
        for (ActivityAttend activityAttend : activityAttendList) {
            Student student = studentDao.findStudentById(activityAttend.getStudentId());
            list.add(student);
        }
        return list;
    }

    /**
     *  插入活动参与表
     * @param activityId
     * @param studentId
     */
    @Transactional
    public void insertActivityAttend(Integer activityId, Integer studentId) {
        ActivityAttend activityAttend = new ActivityAttend();
        activityAttend.setActivityId(activityId);
        activityAttend.setStudentId(studentId);
        activityAttend.setStatus(0);
        activityAttendDao.insert(activityAttend);
    }

    @Transactional
    public  void setTime(){

        List<Activity> all = activityDao.findAll();
        for (Activity activity : all) {
            Date startDate = DateUtil.stringToDate2(activity.getStartTime());
            Date endDate = DateUtil.stringToDate2(activity.getEndTime());
            Date currentDate = new Date();
            System.out.println(activity.getActivityName());
            long diffInMillies = startDate.getTime() - currentDate.getTime();
            long diffInHours = TimeUnit.HOURS.convert(diffInMillies, TimeUnit.MILLISECONDS);
            if( diffInHours > 0 && diffInHours < 24){
                ClubMembers clubPresident = clubMemberDao.findClubPresident(activity.getClubId());
                String value = activity.getActivityName()+"活动验证码为"+activity.getActivityCode();
                informationDao.insertInfor(clubPresident.getStudentId(),value,new Date(),1);
                List<ClubMembers> clubAdmin = clubMemberDao.findClubAdmin(activity.getClubId());
                for (ClubMembers clubMembers : clubAdmin) {
                    informationDao.insertInfor(clubMembers.getStudentId(),value,new Date(),1);
                }
            }
            long diff = currentDate.getTime()-endDate.getTime();
            long time = TimeUnit.HOURS.convert(diff,TimeUnit.MILLISECONDS);
            if(time>24 && time<48){
//
            }
        }
    }
}
